package com.difsonapps.android.marker.adapters;
import java.util.ArrayList;
import java.util.List;

import com.difsonapps.android.marker.MultiPhotoSelectActivity;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Album;
import com.difsonapps.android.marker.beans.PhotoUpload;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UploadPhotosGridAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
	private List<PhotoUpload> data = new ArrayList<PhotoUpload>();
	protected ImageLoader imageLoader = ImageLoader.getInstance();
    
	private DisplayImageOptions options;
 
	public UploadPhotosGridAdapter(Activity activity, List<PhotoUpload> photos) {
        this.activity = activity;
		this.data = photos;
		
		options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.stub_image)
		.showImageForEmptyUri(R.drawable.image_for_empty_url)
		.cacheInMemory()
		.cacheOnDisc()
		.build();
	}
 
    @Override
    public int getCount() {
        return data.size();
    }
 
    @Override
    public Object getItem(int location) {
        return data.get(location);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder = null;
 
		if (row == null) {
			inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.photo_upload_item, null);
			holder = new ViewHolder();
			//holder.imageTitle = (TextView) row.findViewById(R.id.text);
			holder.selectedImageView = (ImageView) row.findViewById(R.id.selectedImageView);
			holder.uploadStatusImageView = (ImageView) row.findViewById(R.id.uploadStatusImageView);
			holder.uploadProgressBar = (ProgressBar) row.findViewById(R.id.uploadProgressBar);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}
 
		PhotoUpload item = data.get(position);
		holder.uploadProgressBar.setVisibility(item.getStatus()==PhotoUpload.STATUS_PENDING?View.VISIBLE:View.GONE);
		if(item.getStatus()==PhotoUpload.STATUS_SUCCEED){
			holder.uploadStatusImageView.setImageResource(R.drawable.ic_ok);
		}else if(item.getStatus()==PhotoUpload.STATUS_FAILED){
			holder.uploadStatusImageView.setImageResource(R.drawable.ic_error);
		}
		holder.uploadStatusImageView.setVisibility(item.getStatus()!=PhotoUpload.STATUS_PENDING?View.VISIBLE:View.GONE);
		
		imageLoader.displayImage("file://"+item.getSrc(), holder.selectedImageView, options, new SimpleImageLoadingListener() {
			@Override
			public void onLoadingComplete(Bitmap loadedImage) {
				/*Animation anim = AnimationUtils.loadAnimation(UploadPhotosGridAdapter.this.activity, R.anim.fade_in);
				holder.selectedImageView.setAnimation(anim);
				anim.start();*/
			}
		});
		//holder.imageTitle.setText(item.getTitle());
		//holder.image.setImageUrl(item.getImage(), imageLoader);
		return row;
	}
 
	static class ViewHolder {
		//TextView imageTitle;
		ProgressBar uploadProgressBar;
		ImageView selectedImageView;
		ImageView uploadStatusImageView;
	}

}
